
/**
 * Creat with IntelliJ IDEA
 * Description:带傀儡节点的双向链表
 * User:WZW
 * Date:2021-11-23
 * Time:19:15
 */

class ListNode{
    int val;
    ListNode prev;
    ListNode next;
    public ListNode(int val){
        this.val = val;
    }
}

public class DoubleLinkedList {
    ListNode head;
    ListNode last;

    public DoubleLinkedList(){
        head = new ListNode(-1);
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head.next==null){
            head.next =node;
            node.prev = head;
            last = node;
        }else {
            head.next.prev = node;
            node.prev = head;
            node.next = head.next;
            head.next = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head.next==null){
            head.next = node;
            node.prev = head;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index, int data) {
        //不合法位置 <0 || >size
        if(index<0 || index>this.size()){
            System.out.println("插入位置不合法！");
            return false;
        }
        //头插
        if(index==0){
            this.addFirst(data);
            return true;
        }
        //尾插
        if(index==this.size()){
            this.addLast(data);
            return true;
        }
        //中间位置
        ListNode node = new ListNode(data);
        ListNode cur = head;
        while(index!=0){
            index--;
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next.prev = node;
        cur.next = node;
        node.prev = cur;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val==key){
                if(cur.next==null){
                    cur.prev.next = null;
                    last = cur.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }else {
                cur = cur.next;
            }
        }
        System.out.println("没有找到关键字为key的节点！");
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head.next;
        boolean flg = false;
        while(cur!=null){
            if(cur.val==key) {
                flg = true;
                if (cur.next == null) {
                    cur.prev.next = null;
                    last = cur.prev;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
                cur = cur.next;
        }
        if(flg==false){
            System.out.println("没有找到关键字为key的节点！");
        }
    }

    //得到单链表的长度
    public int size() {
        ListNode cur = head.next;
        int size = 0;
        while(cur!=null){
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void display() {
        if(head.next==null){
            System.out.print("链表为空！");
        }else {
            ListNode cur = head.next;
            while(cur!=null){
                System.out.print(cur.val+" ");
                cur = cur.next;
            }
        }
        System.out.println();
    }

    public void clear() {
        if(last==null){
            System.out.println("链表已为空！");
        }else {
            ListNode cur = head.next;
            ListNode curNext = cur.next;
            while(curNext!=null){
                cur.next = null;
                cur.prev = null;
                cur = curNext;
                curNext = curNext.next;
            }
            last.prev = null;
            head.next = null;
        }
    }
}
